package com.smart.platform.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smart.platform.common.Result;
import com.smart.platform.entity.Analysis;
import com.smart.platform.entity.Score;
import com.smart.platform.entity.Students;
import com.smart.platform.entity.TeaClasses;
import com.smart.platform.mapper.ScoreMapper;
import com.smart.platform.service.AnalysisService;
import com.smart.platform.service.ScoreService;
import com.smart.platform.service.StudentsService;
import com.smart.platform.service.TeaClassesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/analysis")
@Api(tags = "学情分析接口")
public class AnalysisController {
    @Autowired
    private AnalysisService analysisService;

    @Autowired
    private ScoreService scoreService;

    @Autowired
    private TeaClassesService teaClassesService;

    @Autowired
    private StudentsService studentsService;

    @Autowired
    private ScoreMapper scoreMapper;

    /**
     * 获取当前登录教师所管理的班级信息
     *
     * @param request
     */
    @ApiOperation(value = "获取当前登录教师所管理的年级和班级信息", notes = "当前登录教师的工号通过session获取 前端不需要传递信息")
    @GetMapping("/StudentList")
    public Result<List<Students>> ClassesList(HttpServletRequest request) {
        //通过当前登录教师的session获取到的userId（工号）
        String userId = (String) request.getSession().getAttribute("userId");

        LambdaQueryWrapper<TeaClasses> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(TeaClasses::getUserId, userId);

        TeaClasses list = teaClassesService.getOne(queryWrapper);
        LambdaQueryWrapper<Students> StudentQueryWrapper = new LambdaQueryWrapper<>();
        StudentQueryWrapper.eq(Students::getGrade, list.getGrade())
                .eq(Students::getClasses, list.getClasses());

        List<Students> studentsList = studentsService.list(StudentQueryWrapper);
        //返回去的数据中前端需要接受的是grade和classes 用于后续的数据选择
        return Result.success(studentsList);
    }


    @ApiOperation(value = "学生学情分析接口，返回单个学生某次考试成绩信息")
    @GetMapping("/StudentAnalysis")
    public Result<Score> StudentAnalysis(@RequestBody Score score) {
        LambdaQueryWrapper<Score> queryWrapper = new LambdaQueryWrapper<>();

        //添加两个条件，查询单个学生某次考次的成绩数据
        queryWrapper.eq(Score::getUserId, score.getUserId())
                .eq(Score::getState, score.getState());


        //调用scoreService的getOne方法，查询出符合条件的Score对象
        Score StudentScore = scoreService.getOne(queryWrapper);
        return Result.success(StudentScore);
    }


    @ApiOperation(value = "返回现在拥有的考次数据")
    @GetMapping("/state")
    public Result<List<Map<String, Object>>> State() {
        LambdaQueryWrapper<Score> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Score::getState)
                .groupBy(Score::getState)
                .gt(Score::getState, 0)
                .orderByAsc(Score::getState);

        List<Map<String, Object>> maps = scoreMapper.selectMaps(queryWrapper);

        return Result.success(maps);
    }


    @ApiOperation(value = "返回现在拥有的考次数据")
    @GetMapping("/ClassesAnalysis")
    public Result<Analysis> ClassesAnalysis(HttpServletRequest request, String userId, Integer state) {

        //String userId = (String) request.getSession().getAttribute("userId");

        Analysis analysis = scoreService.save(userId, state);

        return Result.success(analysis);
    }


}
